package Interfaces;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import Auxiliary.Sequence_SplitInfo;
import Boundary.UpdateStateTable;
import DataView.Action;
import DataView.Expr;
import DataView.SplitInfo;
import Store.Event;

public class DividiEventi 
{
	JPanel container;
	JPanel centerPanel;
	UpdateStateTable updateStateTable;
	JTextArea origC;
	JTextArea chosC;
	JTextArea restC;
	JTextArea origR;
	JTextArea chosR;
	JTextArea restR;
	Sequence_SplitInfo lista = new Sequence_SplitInfo();
	int moveEv=0;
	int moveCR=0;
	
	public DividiEventi(UpdateStateTable updateST)
	{
		updateStateTable=updateST;
	}
	
	public JPanel showEVCR()
	{
		container = new JPanel();
		container.setLayout(new BorderLayout());
		JLabel l = new JLabel("Split condition/reactions", JLabel.CENTER);
		l.setFont(new Font("Tahoma", Font.BOLD, 20));
		
		container.add(l, BorderLayout.NORTH);
		container.add(getCenterArea(),BorderLayout.CENTER);
		return container;
	}

	private JPanel getCenterArea() 
	{
		centerPanel= new JPanel();
		centerPanel.setLayout(new BorderLayout());
		JPanel northPanel = new JPanel();
		northPanel.setLayout(new GridLayout(1,2));
		JPanel pannelloDividi = new JPanel();
		pannelloDividi.setLayout(new GridLayout(2,1));
		pannelloDividi.add(getChooseCR(true));
		pannelloDividi.add(getChooseCR(false));
		centerPanel.add(pannelloDividi);
		
		centerPanel.add(getPannelloBottoni(), BorderLayout.SOUTH);
		
		return centerPanel;
	}

	private JPanel getChooseCR(boolean isCond) 
	{
		JPanel pannelloPrincipale = new JPanel();
		pannelloPrincipale.setLayout(new GridLayout(1,3));
		JPanel aux1 = new JPanel();
		aux1.setLayout(new BorderLayout());
		JPanel aux2 = new JPanel();
		aux2.setLayout(new BorderLayout());
		JPanel aux3 = new JPanel();
		aux3.setLayout(new BorderLayout());
		String s="Condition"; if(!isCond) s="Reaction";
		aux1.add(new JLabel("Original "+s), BorderLayout.NORTH);
		aux2.add(new JLabel(s+" Used"), BorderLayout.NORTH);
		aux3.add(new JLabel(s+" Remaining"), BorderLayout.NORTH);
		
		if(isCond)
		{
			origC = new JTextArea(updateStateTable.currentEVCR.get(moveEv).getConditionDescription(moveCR));
			chosC = new JTextArea(updateStateTable.currentEVCR.get(moveEv).getConditionDescription(moveCR));
			restC = new JTextArea("");
			origC.setBorder(BorderFactory.createLineBorder(Color.GRAY));
			chosC.setBorder(BorderFactory.createLineBorder(Color.GRAY));
			restC.setBorder(BorderFactory.createLineBorder(Color.GRAY));
			origC.setLineWrap(true);
			origC.setEditable(false);
			chosC.setLineWrap(true);
			restC.setLineWrap(true);
			aux1.add(origC, BorderLayout.CENTER);
			aux2.add(chosC, BorderLayout.CENTER);
			aux3.add(restC, BorderLayout.CENTER);
		}
		else
		{
			origR = new JTextArea(updateStateTable.currentEVCR.get(moveEv).getReactionDescriprion(moveCR));
			chosR = new JTextArea(updateStateTable.currentEVCR.get(moveEv).getReactionDescriprion(moveCR));
			restR = new JTextArea("");
			origR.setBorder(BorderFactory.createLineBorder(Color.GRAY));
			chosR.setBorder(BorderFactory.createLineBorder(Color.GRAY));
			restR.setBorder(BorderFactory.createLineBorder(Color.GRAY));
			origR.setLineWrap(true);
			origR.setEditable(false);
			chosR.setLineWrap(true);
			restR.setLineWrap(true);
			aux1.add(origR, BorderLayout.CENTER);
			aux2.add(chosR, BorderLayout.CENTER);
			aux3.add(restR, BorderLayout.CENTER);
		}
		
		pannelloPrincipale.add(aux1);
		pannelloPrincipale.add(aux2);
		pannelloPrincipale.add(aux3);
		
		return pannelloPrincipale;
	}
	
	private JPanel getPannelloBottoni() 
	{
		JPanel pannello = new JPanel();
		pannello.setLayout(new GridLayout(1,5));
		JButton ok = new JButton("Next");
		ok.addActionListener(new ActionListener() 
		{
			@Override public void actionPerformed(ActionEvent arg0) 
			{
				String desc = updateStateTable.currentEVCR.get(moveEv).getEventDescription();
				Event cr = updateStateTable.main.OPEN_PRJ.eventCondReact.trova_from_desc(desc);
				lista.add(new SplitInfo(cr.getInfo().getCondReact().get(moveCR), new Expr(chosC.getText()), new Expr(restC.getText()), new Action(chosR.getText()), new Action(restR.getText())));
				
				if(updateStateTable.currentEVCR.get(moveEv).CR_size()-1==moveCR)
				{
					moveCR=0;
					if(updateStateTable.currentEVCR.getAll().size()-1==moveEv)
						updateStateTable.splitCR(lista);
					else moveEv++;
				}
				else moveCR++;
				
				refresh();
			}
		});
		pannello.add(new JLabel(""));
		pannello.add(new JLabel(""));
		pannello.add(ok);
		pannello.add(new JLabel(""));
		pannello.add(new JLabel(""));
		
		return pannello;
	}
	
	public void refresh()
	{
		centerPanel.removeAll();
		centerPanel.add(getCenterArea(), BorderLayout.CENTER);
		centerPanel.revalidate();
	}
}
